package com.zby.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.admin.mapper.BrandMapper;
import com.zby.admin.pojo.Brand;
import com.zby.admin.pojo.dto.BrandDto;
import com.zby.admin.pojo.vo.BrandVo;
import com.zby.admin.service.BrandService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BrandServiceImp extends ServiceImpl<BrandMapper, Brand> implements BrandService  {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public boolean insertBrand(BrandDto brandDto, HttpServletRequest request) {
        if (brandDto.getName() == null|| brandDto.getName().isEmpty()){
            throw new RuntimeException("品牌名称不能为空");
        }
//        if (brandDto.getLogo() == null|| brandDto.getLogo().isEmpty()){
//            throw new RuntimeException("品牌logo不能为空");
//        }
        // 品牌名称唯一性检查
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_name", brandDto.getName());
        Brand one = brandMapper.selectOne(queryWrapper);
        if (one != null) {
             throw new RuntimeException("品牌名称已存在");
         }
        Brand  brand = new Brand();
        brand.setBrandName(brandDto.getName());
        brand.setBrandLogo(brandDto.getLogo());
        brand.setBrandDescription(brandDto.getDescription());
        return this.save(brand);

    }

    @Override
    public IPage<BrandVo> getBrandList(Integer page, Integer pageSize) {

        IPage<Brand> brandVoPage = new Page<>(page, pageSize);
        IPage<Brand> result = this.page(brandVoPage, new QueryWrapper<>());

        // 转换 Brand 到 BrandVo
        IPage<BrandVo> voPage = result.convert(brand -> {
            BrandVo vo = new BrandVo();
            // 手动或使用工具类（如 BeanUtils）进行属性拷贝
            vo.setId(brand.getId());
            vo.setName(brand.getBrandName());
            vo.setLogo(brand.getBrandLogo());
            vo.setDescription(brand.getBrandDescription());
            vo.setCreateTime(brand.getCreateTime());
            return vo;
        });
        return  voPage;
    }

    @Override
    public boolean updateData(BrandDto brandDto, HttpServletRequest request) {
        if (brandDto.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        if (brandDto.getName() == null){
            throw new RuntimeException("名称不能为空");
        }
        Brand brand = new Brand();
        brand.setId(brandDto.getId());
        brand.setBrandName(brandDto.getName());
        brand.setBrandLogo(brandDto.getLogo());
        brand.setBrandDescription(brandDto.getDescription());
        return this.updateById(brand);
    }
}
